package com.wf.course.web.controller.ad;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.utils.page.PageBean;
import com.wf.course.model.ad.AdInfo;
import com.wf.course.model.ad.VipCard;
import com.wf.course.model.course.CourseChapter;
import com.wf.course.model.course.CourseInfo;
import com.wf.course.model.goods.GoodsInfo;
import com.wf.course.service.IAdInfoService;
import com.wf.course.service.ICourseChapterService;
import com.wf.course.service.ICourseInfoService;
import com.wf.course.service.IGoodsInfoService;
import com.wf.course.service.IVipCardService;
import com.wf.course.web.Interceptor.LogInfo;
/**
 * 
 * @content 广告列表Controller
 */
@RequestMapping("adInfo")
@Controller
public class AdInfoController {

	@Autowired
	private IAdInfoService adService;
	@Autowired
	private ICourseInfoService courseInfoService;
	@Autowired
	private IGoodsInfoService goodsInfoService;
	@Autowired
	private IVipCardService cardService;
	
	@Autowired
	private ICourseChapterService chapterService;

	/**
	 * 广告列表展示
	 * @param pageNo
	 * @param pageSize
	 * @param adTitle
	 * @param adStatus
	 * @param model
	 * @return
	 */
	@LogInfo("查询广告列表信息")
	@RequestMapping("adList")
	public String adList(@RequestParam(value = "pageNo", required = false) Integer pageNo,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "adTitle", required = false) String adTitle, 
			@RequestParam(value = "adStatus", required = false) String adStatus, 
			Model model) {

		// 组装一个查询条件的map
		Map<String, Object> cons = new HashMap<String, Object>();
		cons.put("adTitle", adTitle);
		if(adStatus==""){
			adStatus=null;
		cons.put("adStatus", adStatus);
		}else{
			cons.put("adStatus", adStatus);	
		}
		PageBean<AdInfo> pageBean = new PageBean<AdInfo>(pageNo, pageSize, null, cons);
		pageBean.setUrl("adInfo/adList");
		this.adService.indexInfo(pageBean);
		model.addAttribute("page", pageBean);
		return "ad/adInfoList";
	}

	/**
	 * 广告修改和新增页面数据初始化
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@LogInfo("加载广告修改和新增页面数据初始化")
	@RequestMapping("pageLoad/{id}")
	public String downLoadById(@PathVariable("id") Integer id, Model model) {
		 
		if (id != null && id != 0) {
			AdInfo adInfo = adService.queryAdInfoById(id);
			model.addAttribute("adInfo", adInfo);
			model.addAttribute("type", "upd");
		}else{
			model.addAttribute("type", "add");
		}
		 List<CourseInfo> courses = courseInfoService.queryCourses();
		 model.addAttribute("courses",courses);
		 
		 
		 List<GoodsInfo> goods = goodsInfoService.queryGoods();
		 model.addAttribute("goods",goods);
		
		 List<VipCard> cards = cardService.queryVipCards();
		 model.addAttribute("cards",cards);
		 
		 List<CourseChapter> chapters = chapterService.queryCourseChapters();
		 model.addAttribute("chapters",chapters);
		 
		return "ad/adInfoEdit";
	}

	/**
	 * 广告新增和修改
	 * @param adInfo
	 * @param imgFile
	 * @param h5File
	 * @param request
	 * @return
	 */
	@LogInfo("修改/新增广告信息")
	@RequestMapping(value = "adInfoEdit" , produces = "text/html;charset=UTF-8")
	public String adInfoEdit(@ModelAttribute AdInfo adInfo,
			MultipartFile imgFile,MultipartFile h5File,  HttpServletRequest request) {
		
		adService.saveOrUpdateAdInfo(adInfo, imgFile, h5File);
		
		return "redirect:/adInfo/adList";
	}

	/**
	 * 广告删除
	 * @param id
	 * @return
	 */
	@LogInfo("删除广告信息")
	@RequestMapping(value = "remove/{id}")
	public String removeAd(@PathVariable("id") Integer id) {
		
		adService.deleteById(id);
		
		return "redirect:/adInfo/adList";
	}
	
	/**
	 *广告位置顶
	 * 
	 * @param id
	 * @return
	 */
	@LogInfo("广告位置顶")
	@RequestMapping("AdInfoTop/{id}")
	public String AdInfoTop(@PathVariable("id") Integer id) {
		 
		if (id != null && id != 0) {
			adService.AdInfoTop(id);
		}
		
		return "redirect:/adInfo/adList";
	}
}
